"""Volcanoplot

.. helpdoc::

Generates a volcano plot based on an affy data set.
"""

"""<widgetXML>
    <name>Volcanoplot</name>
    <icon>Default.png</icon>
    <tags> 
        <tag>Microarray</tag> 
    </tags>
    <summary>Generates a volcano plot of affy data.</summary>
    
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""


"""
<name>Volcanoplot</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<description>Makes a volcanoplot from a differential expression fit.</description>
<RFunctions>limma:volcanoplot</RFunctions>
<tags>Array Analysis</tags>
<icon></icon>
"""
from OWRpy import * 
import redRGUI 
import libraries.base.signalClasses as signals
import libraries.affy.signalClasses as affySignals

class RedRvolcanoplot(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(["limma"])
        self.RFunctionParam_names = ''
        self.RFunctionParam_fit = ''
        
        """.. rrsignals::"""
        self.inputs.addInput('names', "Name List", signals.RVector.RVector, self.processnames)
        
        """.. rrsignals::"""
        self.inputs.addInput("fit", 'Array Model Fit', affySignals.RMArrayLM.RMArrayLM, self.processfit)
        

        """.. rrgui::"""
        self.RFunctionParamxlab_lineEdit = redRLineEdit(self.controlArea, label = "X Label:", text = "LogFoldChange")
        
        """.. rrgui::"""
        self.RFunctionParamylab_lineEdit = redRLineEdit(self.controlArea, label = "Y Label:", text = "LogOdds")
        
        """.. rrgui::"""
        self.RFunctionParamcoef_lineEdit = redRLineEdit(self.controlArea, label = "coef:", text = '1')
        
        """.. rrgui::"""
        self.RFunctionParamhighlight_lineEdit = redRLineEdit(self.controlArea, label = "highlight:", text = '0')
        
        self.plotArea = redRgraphicsView(self.controlArea)
        redRCommitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processnames(self, data):
        if data:
            self.RFunctionParam_names=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_names=''
    def processfit(self, data):
        if data:
            self.RFunctionParam_fit=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_fit=''
    def commitFunction(self):

        if unicode(self.RFunctionParam_fit) == '': 
            self.status.setText('No Data To Plot')
            return
        if unicode(self.RFunctionParam_names) == '': self.RFunctionParam_names = unicode(self.RFunctionParam_fit)+'$genes$ID'
        injection = []
        if unicode(self.RFunctionParamhighlight_lineEdit.text()) != '':
            string = 'highlight='+unicode(self.RFunctionParamhighlight_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamxlab_lineEdit.text()) != '':
            string = 'xlab='+unicode(self.RFunctionParamxlab_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamylab_lineEdit.text()) != '':
            string = 'ylab='+unicode(self.RFunctionParamylab_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamcoef_lineEdit.text()) != '':
            string = 'coef='+unicode(self.RFunctionParamcoef_lineEdit.text())+''
            injection.append(string)
        inj = ','.join(injection)
        self.plotArea.plot(function = 'volcanoplot', query = 'names='+str(self.RFunctionParam_names)+',fit='+str(self.RFunctionParam_fit)+','+inj)
